/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * Navigator Bldg, ITPL, Bangalore, India.
 */

package org.or5e.api.liveradio;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import org.or5e.core.BaseObject;

/**
 * This the the Shout Channel Implementaion. This is the Default implementaion
 * support by the Live Radio. This is the Startup implementaion moving forward
 * this API will come with more radio provider support.
 * @author Ponraj Suthanthiramani
 * @version 1.0b
 */
public class ShoutChannel extends BaseObject implements LiveRadioChannel{
    private Properties _channelProps;
    /**
     * This is the construction process, which needs the property file from were
     * we need to read the Radio Station information.
     * @param propertyFile
     * @throws IOException
     */
    public ShoutChannel(String propertyFile) throws IOException {
        _channelProps = new Properties();
        _channelProps.load(new FileInputStream(propertyFile));
    }

    @Override
    public String getName() {
        return "ShoutChannel";
    }

    /**
     * This will add the Channel inforation to the Cache database. This will add only
     * the Channels streamed using the shout provider.
     * @return
     */
    @Override
    public Map<String, RadioInfo> initChannels() {
        Map<String, RadioInfo> initilizedChannels = new HashMap<String, RadioInfo>();
        Set<Object> keySet = _channelProps.keySet();
        for (Object key : keySet) {
            try {
                Map<String, String> radioInfoKeyValuePair = new HashMap<String, String>();
                String shoutURL = _channelProps.get(key).toString();
                if(shoutURL.indexOf("yp.shoutcast.com") == -1) {
                    continue;
                }
                URL radioURL = new URL(shoutURL);
                URLConnection urlCon = radioURL.openConnection();
                BufferedInputStream inStream = new BufferedInputStream(urlCon.getInputStream());
                int availableSize = inStream.available();
                byte[] urlData = new byte[availableSize];
                inStream.read(urlData);

                String urlContent = new String(urlData);
                StringTokenizer tokenizer = new StringTokenizer(urlContent, "\n");
                while(tokenizer.hasMoreTokens()) {
                    StringTokenizer lineToken = new StringTokenizer(tokenizer.nextToken(), "=");
                    if(lineToken.countTokens() == 2) {
                        radioInfoKeyValuePair.put(lineToken.nextToken(),lineToken.nextToken());
                    }
                }

                RadioInfo radInfo = new RadioInfo(radioInfoKeyValuePair.get("numberofentries"), new URL(radioInfoKeyValuePair.get("File1")), radioInfoKeyValuePair.get("Title1"), Integer.parseInt(radioInfoKeyValuePair.get("Length1")), radioInfoKeyValuePair.get("Version"));
                initilizedChannels.put(key.toString(), radInfo);
            }
            catch(Exception e) {

            }
        }
        return initilizedChannels;
    }
}